package cmdclasses.bandit;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Timer;

import org.json.JSONObject;

public class CurrentPlayed {
	public static String fetchUrl = "http://www.bandit.se/sites/bandit.se/files/radio_song/current_song.json";
	public String currentSong;
	public Timer timer;

	public CurrentPlayed() {
	}

	public static String currentSong() {
		String toSend = "";
		try {
			String data = getJsonString();
			String stuff = data.substring(18, data.length()-1);
			JSONObject json = new JSONObject(stuff);
			String artist = (String) json.get("artist");
			String song = (String) json.get("title");
			toSend = "Now playing: " + song + " | " + artist;
		} catch (Exception e) {
			toSend = "Error parsing";
		}
		return toSend;
	}

	private static String getJsonString() throws MalformedURLException,
			IOException {
		String data = "";
		URL url = new URL(fetchUrl);
		BufferedReader in = new BufferedReader(new InputStreamReader(
				url.openStream()));
		String inputLine;
		while ((inputLine = in.readLine()) != null)
			data += inputLine;
		in.close();
		return data;
	}
}